package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.service.TestService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@Api("TestController")
@RequestMapping(value = "/consumer2")
// 配置hystrix 全局fallback
@DefaultProperties(defaultFallback = "payment_Global_Fallback" )
public class TestController2 {
	@Autowired
	TestService testService;
	@Value("${server.port}")
	private String serverPort;
	
	
	@ApiOperation("查询")
	@GetMapping(value = "/t/test1")
	public CommonResult test1() {
		log.info("调用开始：" + serverPort);
		return testService.test1();
	}
	

	@ApiOperation("查询")
	@GetMapping(value = "/t/test2")
	@HystrixCommand(fallbackMethod = "payment_TimeOutHandler" , commandProperties = {
			 @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000")
	 })
	public CommonResult test2() {
		log.info("调用开始：" + serverPort);
		return testService.test2();
	}
	 public CommonResult payment_TimeOutHandler() {
		 return CommonResult.error("对方系统响应超时");
	}
	 
	 
	@ApiOperation("查询")
	@GetMapping(value = "/t/test3")
	@HystrixCommand()
	public CommonResult test3() {
		log.info("调用开始：" + serverPort);
		return testService.test2();
	}
	 
	 public CommonResult payment_Global_Fallback() {
		 return CommonResult.error("对方系统响应异常");
	 }
}
